const baseURL = 'http://43.139.222.133:7001/'
const ui = require("./loading")

function request({
    method,
    url,
    data = {},
    contentType,
    message,
    showLoading = false
}) {
    return new Promise((resolve, reject) => {
        if (showLoading) {
            ui.showLoading(message ? message : '加载中..')
        }
        wx.request({
            url: baseURL + url,
            method,
            data,
            header: {
                'content-Type': contentType
            },
            success(res) {
                resolve(res.data)
            },
            fail(err) {
                console.log(err)
                reject("服务器连接异常，请检查网络再试");
            },
            complete() {
                ui.hideLoading()
            }
        })
    })
}
const uploadFile = (file, formData = {}) => {
    return new Promise((resolve, reject) => {
        wx.uploadFile({
            url: baseURL + 'uploadFile',
            filePath: file.url,
            name: file.name,
            formData,
            success(res) {
                resolve(res)
            },
            complete(res) {
            }
        })
    })
}

module.exports = {
    request,
    uploadFile

}